# -*-coding: Utf-8 -*-
# @File : src .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time：2023/6/10

from interface import user_interface
from lib import common

function_menus = '''
 ===========功能菜单=============
           1.注册功能
           2.登录功能
           3.查看全部用户信息
           4.查看指定用户信息
 =============END=============

'''

login_user = None


@common.count_time
def register():
    while True:
        print('欢迎来到注册功能!')
        username = input('请输入用户名:>>>>').strip()
        password = input('请输入密码:>>>>').strip()
        age = input('请输入你的年龄:>>>>').strip()
        if not age.isdigit():
            continue
        love_music = input('请输入你喜欢的音乐:>>>>').strip()
        love_sport = input('请输入你喜欢的运动项目:>>>>').strip()
        flag, msg = user_interface.register_interface(username, password, age, love_music, love_sport)
        if flag:
            print(msg)
            break
        else:
            print(msg)
            cmd = input('是否继续注册?(q退出)').strip()
            if cmd == 'q':
                print('已退出注册')
                break


@common.count_time
def login():
    while True:
        print('欢迎来到登陆功能!')
        username = input('请输入用户名:>>>>').strip()
        password = input('请输入密码:>>>>').strip()
        random_code = common.get_random_code(6)
        print('请输入随机验证码进行登陆(可不区分大小写)', random_code)
        code_input = input('请输入验证码进行登录').strip()
        flag, msg = user_interface.login_interface(username, password, random_code, code_input)
        if flag:
            global login_user
            login_user = username
            print(login_user)
            print(msg)
            break
        else:
            print(msg)
            cmd = input('是否继续登陆?(q退出)').strip()
            if cmd == 'q':
                print('已退出注册')
                break


@common.login_check
def check_all_info():
    while True:
        user_interface.check_all_info_interface()
        cmd = input('是否继续查询?(q退出)').strip()
        if cmd == 'q':
            print('已退出注册')
            break
        else:
            continue

@common.login_check
def check_one_info():
    while True:
        username_input = input('请输入要查询的用户名:>>>').strip()
        user_interface.check_one_info_interface(username_input)
        cmd = input('是否继续查询?(q退出)').strip()
        if cmd == 'q':
            print('已退出注册')
            break
        else:
            continue

function_dict = {
    '1': register,
    '2': login,
    '3': check_all_info,
    '4': check_one_info,
}


def main():
    while True:
        # 打印功能菜单
        print(function_menus)
        # 输入功能ID
        choice = input('请输入功能编号:>>>>').strip()
        # 功能ID不是数字则继续输入
        if not choice.isdigit():
            continue
        if choice not in function_dict.keys():
            print('请输入正确的功能编号!')
            continue

        # function_dict[1]() ----> register()
        function_dict[choice]()
